<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Post;
use App\Type;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{


   // 文章类型
   public function types(){
       return Type::all();
   }

    //文章列表
    public function list(Request $request){

        $size = $request->size ? $request->size:5;
        if($request->type_id == 0 || !$request->type_id){
            $posts = Post::orderBy('id','desc')->paginate($size); //分页每页5条
        }else{
            $posts = Post::where('type_id',$request->type_id)->orderBy('id','desc')->paginate($size); //分页每页5条
        }
        
        return $posts;
    }

    // 当前用户收藏的文章
    public function m_myfav(){
        $posts=Auth::user()->favorites;
        return $posts->all();
    }

    // 所有的文章
    public function mlist(){
        //$types = Type::all(); // 所有的記錄
        $posts = Post::paginate(10); //分页每页10条
        return $posts->all();
    }

    // 文章详情
    public function m_detail($id){
        $post=Post::find($id);
        return $post;
    }


    // 文章检索
    public function m_search($words){
       // $post=Post::find($id);
        $posts = Post::where("title","like","%".$words."%")->paginate(10);
        return $posts;
    }


    // 分类文章
    public function type($id){
        $words="";
        $types = Type::all(); // 所有的記錄
        $posts = Post::where("type_id",$id)->paginate(5); //分页每页5条
        return view('post/type', compact('posts',"types","id","words"));

    }

    //收藏文章
    public function favorite($id){

        // 1 user_id;
        // 2 $post_id
        // 3 user_id post_id 保存到数据库中

       Auth::user()->favorites()->attach($id);
      // dd(Auth::user()->favorites->toArray());

    }

    //取消收藏
    public function unfavorite($id){
        Auth::user()->favorites()->detach($id);
    }

    // 发布文章
    public function write(){

        $types = Type::all(); // 所有的記錄
        $mark="write";
        return view('post/write',compact("types","mark"));
    }

    // 发布文章
    public function save(Request $request){

        $user_id = \Auth::guard('api')->id();

        if(!$user_id){
            return response()->json(['status'=>3000,'msg'=>'请先登录!']);
        }
        
        $data = $request->toArray();
        $data["user_id"] =  $user_id;
        $data["state"] = 1;
        
        Post::create($data);
        
        return response()->json(['status'=>200,'msg'=>'发布成功!','data'=>$data]);
        
    }

    // 我的文章
    public function mypost(){

        $posts=Auth::user()->posts;
        //$posts = Post::where("user_id",Auth::id())->get();
        return view("post/mypost",compact("posts"));
    }


    //编辑文章
    public function edit($id){
       $post=Post::find($id);
       $types = Type::all();
       return view("post/edit",compact("post","types"));
    }

    // 更新文章
    public function update(Request $request){
        $data = $request->except("_token");
        $data["user_id"]=Auth::id();

        $post = Post::find($data["id"]);// 当前文章实例化对象

        $post-> title = $data["title"];
        $post-> body = $data["body"];
        $post-> type_id = $data["type_id"];
        $post-> state = $data["state"];

        $post->save();

        return redirect("post/mypost");

    }


    // 删除文章
    public function delete($id){

        Post::destroy($id);
        return redirect("post/mypost");
    }

    // 我的收藏
    public function myfav(){
        // users favorites  posts
        // user_id  post_id

        //1 获取登录用户收藏所有文章 ;
        //2 收藏文章  取消收藏

        $posts=Auth::user()->favorites;
        // Auth::user()->favorites()->attach($id);
        // Auth::user()->favorites()->detach($id);

        return view("post/myfav",compact("posts"));

    }

    public function detail($id){
        $post=Post::find($id);
        return view("post/detail",compact("post"));

    }

    // 文章搜索
    public function search(Request $request){
        $words = $request->words;
        $posts = Post::where("title","like","%".$words."%")->paginate(5);
        $types=Type::all();
        return view("post/list",compact("posts","types","words"));
    }


}

